package cn.kgc.controller;

import cn.kgc.common.CrowdFundingConstant;
import cn.kgc.common.ResultEntity;
import cn.kgc.pojo.Hstaff;
import cn.kgc.service.HstaffService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class StaffController {
    @Resource
    HstaffService hstaffService;


    @RequestMapping("/staff/pageinfo/name")
    public String  StaffpageInfo(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                          @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                          Model model){
        PageInfo<Hstaff> pageInfo = hstaffService.queryForKeywordSearchHstaff(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return  "body/housestaff/hstaff-member-list";
    }


    //详细信息
    @RequestMapping("/select/xiang/staff")
    public String selectxiangstaff(Integer id,Model model){
        Hstaff hstaff = hstaffService.selectByIdHstaffXiang(id);
        model.addAttribute("b",hstaff);
        return "body/housestaff/hstaff-xiang";
    }


    @RequestMapping("/insert/staff")
    // 添加 ok
    public String insertHousestaff(Hstaff hstaff) {
        hstaffService.insertHousestaff(hstaff);
        return "redirect:/staff/pageinfo/name";//添加成功返回查询页面
    }

    //修改回显
    @RequestMapping("/select/toupdate/staff")
    public String selecttoupdatestaff(Integer id,Model model){
        Hstaff hstaff = hstaffService.selectByIdHstaffToUpdatge(id);
        model.addAttribute("b",hstaff);
        return "body/housestaff/hstaff-update";
    }
    //  修改 ok
    @RequestMapping("/select/update/staff")
    public String updateHousestaff(Hstaff hstaff,Integer pageNum) {
        hstaffService.updateHousestaff(hstaff);
        //当前页 存储过去
        return "redirect:/staff/pageinfo/name?pageNum="+pageNum;
    }

    @RequestMapping("/delete/byId/staff")
    public String deleteByIdstaff(int id) {
        hstaffService.deleteStaff(id);
        return "redirect:/staff/pageinfo/name";
    }


    @ResponseBody
    @RequestMapping("/delete/staff")
    //批量删除 ok
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> hstaffIdList,Integer pageNum) {
        hstaffService.getUserListByIdList(hstaffIdList);
        return ResultEntity.successWithoutData();
    }






}
